<?php
/**
 * ViewHeader.php
 * 
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/** Zend_Controller_Plugin_Abstract */
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Naneau_Controller_Plugin_ViewHeader
 * 
 * automagically set up content-type headers
 *
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Plugin_ViewHeader extends Zend_Controller_Plugin_Abstract {

    /**
     * shutdown hook
     */
    public function dispatchLoopShutdown() {
        if (Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
            $viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');
            $view = $viewRenderer->view;
            //get the view out of viewRenderer

            if (!$viewRenderer->getNeverRender() && $view instanceof Naneau_View) {
                //yay! somebody used Naneau_View!

                $encoding = $view->getEncoding();
                //the encoding the view uses (let's hope it's utf-8)
                
                switch ($view->getContentType()) {
                    case Naneau_View::XHTML:
                        if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
//                            $contentType = 'application/xhtml+xml';
                            $contentType = 'text/html';
                        }
                        else {
                            $contentType = 'text/html';
                        }
                }

                $this->getResponse()->setRawHeader("Content-type: $contentType; charset=$encoding");
                //set the header
            }
        }
    }
}